package com.gump.essay.demo.design;

import com.gump.essay.demo.design.cxgc.AppleFruitFactory;
import com.gump.essay.demo.design.facade.Takeaway;
import com.gump.essay.demo.design.facade.TakeawayService;
import com.gump.essay.demo.design.gczms.Author;
import com.gump.essay.demo.design.gczms.Blog;
import com.gump.essay.demo.design.gczms.Comment;
import com.gump.essay.demo.design.jdgc.Fruit;
import com.gump.essay.demo.design.jdgc.FruitFactory;
import com.gump.essay.demo.design.jzz.Shop;
import com.gump.essay.demo.design.proxy.DynamicProxy;
import com.gump.essay.demo.design.proxy.IceCreamService;
import com.gump.essay.demo.design.proxy.IceCreamServiceImpl;
import com.gump.essay.demo.design.spq.Pie;
import com.gump.essay.demo.design.spq.RespberryPieAdaptor;
import com.gump.essay.demo.design.yxms.Student;
import com.gump.essay.demo.design.zszms.AbstractFruitSalad;
import com.gump.essay.demo.design.zszms.FruitSalad;
import com.gump.essay.demo.design.zszms.KiwiDecorator;
import com.gump.essay.demo.design.zszms.WaterMelonDecorator;

import java.util.ArrayList;

/**
 * @auther zzc
 * @date 2020/12/9 11:52
 */
public class DesignApplication {

    public static void main(String[] args) throws CloneNotSupportedException {

        /*-------------------创建型模式-----------------------------*/
        //简单工厂
        FruitFactory fruitFactory = new FruitFactory();
        Fruit apple = fruitFactory.produce("apple");
        apple.eat();

        //抽象工厂
        AppleFruitFactory appleFruitFactory = new AppleFruitFactory();
        appleFruitFactory.getFruit().buy();
        appleFruitFactory.getPrice().pay();

        //建造者模式 （生成器模式）   类似 @Builder
        Shop shop = new Shop.ShopBuilder().name("xxx水果店").location("鄱阳大桥头").type("水果经营").build();
        System.out.println(shop.toString());

        //单例模式懒汉

        //原型模式
        Student student = new Student();
        ArrayList<Student> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Student s = (Student) student.clone();
            s.setAge(20 + i);
            s.setName("学生" + i);
            list.add(s);
        }
        System.out.println(list);
        /*-------------------创建型模式-----------------------------*/



        /*-------------------结构型模式-----------------------------*/
        //外观模式  （门面模式）
        Takeaway takeaway = new Takeaway();
        takeaway.setName("麻辣香锅");
        TakeawayService takeawayService = new TakeawayService();
        takeawayService.takeOrder(takeaway);

        //装饰者模式 使用装饰器
        //点了份水果沙拉  两个猕猴桃 一个西瓜
        AbstractFruitSalad fruitSalad = new FruitSalad();
        fruitSalad = new WaterMelonDecorator(new KiwiDecorator(new KiwiDecorator(fruitSalad)));
        System.out.println(fruitSalad.remark() + "价格是：" + fruitSalad.price());

        //适配器模式
        Pie pie = new RespberryPieAdaptor();
        pie.make();

        //代理模式
        //静态代理      增强目标对象
        //jdk动态代理   只能代理接口
        IceCreamService proxy = (IceCreamService) new DynamicProxy(new IceCreamServiceImpl()).proxy();
        proxy.makeIceCream("草莓");
        //CGlib代理
        /*
        spring:
            aop:
                proxy-target-class: true
         */
        /*-------------------结构型模式-----------------------------*/


        /*-------------------行为型模式-----------------------------*/
        //观察者模式
        Blog blog = new Blog("java从入门到放弃");
        Author author = new Author("gump");

        //添加观察者
        blog.addObserver(author);

        Comment comment = new Comment("gump", "还好及时看到楼主的博客，让我放弃了java，回家继承亿万家产");
        blog.comment(comment);



    }
}
